<?php

namespace App\Store\Controllers\Account;

use App\Common\Services\Store\Common;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Account\Account;
use App\Store\Models\Account\AccountDepartment;
use App\Store\Models\Account\SiteStore;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Store\Services\AccountMenuRole;
use App\Store\Validate\Account\AccountDepartment as ValidateAccountAccountDepartment;

/**
 * 部门管理
 */
class AccountDepartmentController extends Controller
{

    /**
     * 列表
     */
    public function getlist(Request $request)
    {
        $list = AccountDepartment::getlist($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    public function dopostadd(Request $request)
    {
        $req_data = $request->only(['d_name', 'remark']);
        $Validate = new ValidateAccountAccountDepartment();
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        //判断部门名称是否重复
        $isexist = AccountDepartment::where([['d_name', $req_data['d_name']]])->first();
        if ($isexist) {
            return response()->json(['code' => '001', 'msg' => '部门名称不允许重复']);
        }

        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $data = $req_data + ['cp_id' => $cp_id, 'create_at' => time()];
        AccountDepartment::create($data);
        return response()->json(['code' => '000', 'msg' => '新增成功']);
    }

    public function dopostedit(Request $request)
    {
        $req_data = $request->only(['id', 'd_name', 'remark']);
        $Validate = new ValidateAccountAccountDepartment();
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        //判断部门名称是否重复
        $isexist = AccountDepartment::where([['id', '<>', $req_data['id']], ['d_name', $req_data['d_name']]])->first();
        if ($isexist) {
            return response()->json(['code' => '001', 'msg' => '部门名称不允许重复']);
        }

        $data = [
            'd_name' => $req_data['d_name'],
            'remark' => $req_data['remark'],
            'update_at' => time()
        ];
        AccountDepartment::where('id', $req_data['id'])->update($data);
        return response()->json(['code' => '000', 'msg' => '编辑成功']);
    }

    /**
     * 新增部门管理员
     */
    public function dopostaddaccount(Request $request)
    {
        // type=12 财务部
        $req_data = $request->only(['d_id', 'store_ids', 'account_name', 'real_name', 'user_no', 'is_band_userimg','user_img','is_order_msg','is_sendorder_msg', 'phone_no','mobile','status','entry_at','salary','remark']);
        $Validate = new ValidateAccountAccountDepartment('addaccount');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        //判断账号是否重复
        $isexist = Account::where([['account_name', $req_data['account_name']]])->first();
        if ($isexist) {
            return response()->json(['code' => '001', 'msg' => '账号不允许重复']);
        }

        $data=$req_data;
        foreach ($data as $key => $val) {
            if ($val == '' || $val == 0) {
                unset($data[$key]);
            }
        }
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $data['type'] = 12;
        $data['is_department'] = 2;
        $data['cp_id'] = $cp_id;
        $data['password'] = MD5(MD5('zhuxiaobao_' . env('ACCOUNT_DEF_PWD')));
        $data['entry_at'] = isset($data['entry_at'])?strtotime($data['entry_at']):0;
        $data['create_at'] = time();
        $account_id=Account::create($data)->id;
        AccountMenuRole::setAccountRole($account_id, 12);

        return response()->json(['code' => '000', 'msg' => '新增管理员账号成功']);
    }

    /**
     * 部门管理员列表
     */
    public function getlistaccount(Request $request)
    {
        $req_data = $request->only(['d_id']);
        $Validate = new ValidateAccountAccountDepartment('getlistaccount');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $list = Account::getlist($request);
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        foreach($list['list'] as &$val){
            if($val['store_ids']==''){
                $val['store_listinfo']=SiteStore::where(['cp_id'=>$cp_id])->select(['id','store_name'])->get()->toArray();
            }else{
                if(is_numeric($val['store_ids']) && intval($val['store_ids'])>0){
                    $val['store_listinfo']=SiteStore::where(['id'=>intval($val['store_ids']),'cp_id'=>$cp_id])->select(['id','store_name'])->get()->toArray();
                }else{
                    $val['store_listinfo']=SiteStore::whereIn('id',explode(',',$val['store_ids']))->where(['cp_id'=>$cp_id])->select(['id','store_name'])->get()->toArray();
                }
            }
        }
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    public function dodel(Request $request)
    {
        $req_data = $request->only(['id']);
        $Validate = new ValidateAccountAccountDepartment('del');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        //删除部门
        AccountDepartment::where('id', $req_data['id'])->update(['is_del' => 2]);
        $request->d_id = $req_data['id'];
        $request->isgetall = 1;
        $list = Account::getlist($request);

        //删除部门下人员
        Account::where('d_id', $req_data['id'])->update(['is_del' => 2]);

        //循环删除人员登录状态
        if ($list['list']) {
            foreach ($list['list'] as $item) {
                Common::cleartokenkey($item['id']);
                Common::cleartokenkeybywxapp($item['id']);
            }
        }
        return response()->json(['code' => '000', 'msg' => '删除成功']);
    }
}
